//
//  LSDownloadManager.h
//  LibraryStorm
//
//  Created by Anh Nguyen on 3/31/14.
//  Copyright (c) 2014 3ATeam. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AFDownloadRequestOperation.h"
@protocol LSDownloadManagerDelegate;

typedef enum {
    LSObjectDownloadNone = 0,
    LSObjectDownloading = 1,
    LSObjectDownloadPause = 2,
    LSObjectDownloadFailed = 3,
    LSObjectDownloadCompleted = 4,
} LSObjectDownloadStatus;

@interface LSDownloadManager : NSObject
// How many file can download at the same time.
// Default 3.
@property (assign, nonatomic) int maxRunningTaskCount;
// Should try to resume download proccess if there are download temp file exsist.
// Only affect operations which created after this property set.
// Default YES.
@property (assign, nonatomic) BOOL shouldResume;
@property (nonatomic, weak) id <LSDownloadManagerDelegate> delegate;

+ (LSDownloadManager *)sharedInstance;
/// Return nil, if has the url or operation creat failure.
- (AFDownloadRequestOperation *)addURL:(NSURL *)url fileStorePath:(NSString *)destinationFilePath;
- (AFDownloadRequestOperation *)findOperationWithURL:(NSURL *)url;
/// Subclass can override this method to change operation option.
- (void)setupDownloadOperation:(AFDownloadRequestOperation *)downloadOperation;

- (void)startAll;
- (void)pauseAll;
- (void)cancelAll;
- (BOOL)startOperation:(AFDownloadRequestOperation *)operation;
- (BOOL)pauseOperation:(AFDownloadRequestOperation *)operation;
- (BOOL)cancelOperation:(AFDownloadRequestOperation *)operation;
- (BOOL)startOperationWithURL:(NSURL *)url;
- (BOOL)pauseOperationWithURL:(NSURL *)url;
- (BOOL)cancelOperationWithURL:(NSURL *)url;

- (NSSet *)downloadingOperations;
- (NSSet *)operationsInQueue;
- (NSSet *)pausedOperations;
@end

@protocol LSDownloadManagerDelegate <NSObject>

@optional
// Successful Download
- (void)downloadManager:(LSDownloadManager *)downloadManager
     operationCompleted:(AFHTTPRequestOperation *)operation;
// Failed Download
- (void)downloadManager:(LSDownloadManager *)downloadManager
        operationFailed:(AFHTTPRequestOperation *)operation;
// Status Download: Update, Process, Speed
- (void)downloadManager:(LSDownloadManager *)downloadManager
   operationStateUpdate:(AFDownloadRequestOperation *)operation
       downloadProgress:(float)progress;
@end
